package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * This message confirms the initiation of a call set-up. This connection
 * is assigned a PLCI, which serves as an identifier in further processing.
 * Errors are returned in the parameter Info.
 */
public class ConnectConf
        extends CapiPassiveMessage
        implements InfoAccessor, PlciAccessor {

    /** Location of the PLCI parameter within the message. */
    private static final int OFFSET_PLCI = OFFSET_MESSAGE;

    /** Location of the Info parameter within the message. */
    private static final int OFFSET_INFO = OFFSET_MESSAGE + SIZE_OF_PLCI;

    /**
     * Create a new {@code ConnectConf} from the specified
     * {@code ByteBuffer}.
     *
     * @param byteBuffer A {@code ByteBuffer} containing a CONNECT_CONF message.
     */
    public ConnectConf(final ByteBuffer byteBuffer) {
        super(byteBuffer);
    }

    /** {@inheritDoc} */
    public int getPlci() {
        return getByteBuffer().getInt(OFFSET_PLCI);
    }

    /** {@inheritDoc} */
    public ErrorInfo getInfo() {
        return ErrorInfo.valueOf(getByteBuffer().getShort(OFFSET_INFO));
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format("%s[%s plci=0x%08x info=%s]",
                getClass().getSimpleName(),
                super.toString(),
                getPlci(),
                getInfo());

        return s;
    }

}
